package com.teatang.tadmin.sysuser.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.teatang.tadmin.core.page.BaseReturn;
import com.teatang.tadmin.core.page.PageDataReturn;
import com.teatang.tadmin.sysuser.mapper.SysUserMapper;
import com.teatang.tadmin.sysuser.pojo.SysUser;
import com.teatang.tadmin.sysuser.pojo.SysUserSearch;
import com.teatang.tadmin.sysuser.vo.SysUserVo;
import com.teatang.tadmin.util.PageUtils;
import com.teatang.tadmin.util.PrintUtils;
import com.teatang.tadmin.util.ServletUtils;
import com.teatang.tadmin.util.StringUtils;
import com.teatang.tadmin.util.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageDataReturn getSysUsersPageData() {
        String searchParamsStr = ServletUtils.getParameter("searchParams", "");
        SysUserSearch sysUserSearch = new SysUserSearch();
        if(!StringUtils.isEmpty(searchParamsStr)) {
            sysUserSearch = JSON.parseObject(searchParamsStr, SysUserSearch.class);
//            PrintUtils.printJson(sysUserSearch);
        }
        List<SysUser> sysUserPageList = sysUserMapper.getSysUserPageList(sysUserSearch);
        List<SysUserVo> sysUserVos = new ArrayList<>();
        for (SysUser sysUser : sysUserPageList) {
            SysUserVo sysUserVo = new SysUserVo();

            sysUserVo.setId(sysUser.getId());
            sysUserVo.setName(sysUser.getName());
            sysUserVo.setEmail(sysUser.getEmail());
            sysUserVo.setPhone(sysUser.getPhone());
            sysUserVo.setRoleId(sysUser.getRoleId());
            sysUserVo.setCreateAt(sysUser.getCreateAt());
            sysUserVo.setIsDisplay(sysUser.getIsDisplay());

            sysUserVos.add(sysUserVo);
        }

        return PageUtils.getDataPage(sysUserVos, new PageInfo(sysUserPageList).getTotal());
    }

    @Override
    public SysUser getSysUserById(int id) {
        return sysUserMapper.getSysUserById(id);
    }

    @Override
    public BaseReturn insertSysUser(SysUser sysUser) {
        int status = sysUserMapper.insertSysUser(sysUser);
        return PageUtils.success(String.valueOf(status));
    }

    @Override
    public BaseReturn updateSysUser(SysUser sysUser) {
        int status = sysUserMapper.updateSysUser(sysUser);
        return PageUtils.success(String.valueOf(status));
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public BaseReturn deleteSysUserByIds(String ids) {
        Long[] userIds = Convert.toLongArray(ids);
//        for (Long userId : userIds)
//        {
//            checkUserAllowed(new SysUser(userId));
//        }
        // 删除用户与角色关联
//        userRoleMapper.deleteUserRole(userIds);
        // 删除用户与岗位关联
//        userPostMapper.deleteUserPost(userIds);
        int flag = sysUserMapper.deleteUserByIds(userIds);
        return PageUtils.success(String.valueOf(flag));
    }
}

